package com.zzhou.linkinfo.clock.activity.base;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;
import com.zzhou.linkinfo.clock.R;
import com.zzhou.linkinfo.clock.mvp.IBasicView;

/**
 * <desc>
 * Created by The Moss on 2019/3/12.
 */

public abstract class AbstractTitleActivity extends RxAppCompatActivity implements IBasicView{
    protected String TAG = this.getClass().getSimpleName();
    protected ZLoadingDialog mDialog;
    View rootView;
    public Toolbar _mToolbar;
    private FrameLayout _mContentLayout;

    protected abstract int attachLayoutRes();
    protected abstract void initView();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rootView = View.inflate(this, R.layout.clock_basic_title_activity,null);
        _mToolbar = rootView.findViewById(R.id._toolbar);
        _mContentLayout = rootView.findViewById(R.id._content);
        _mToolbar.setTitle("");
        setSupportActionBar(_mToolbar);
        setBackBar(true);
        addContentView(attachLayoutRes());
    }
    public void setBackBar(boolean isShow) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(isShow);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return onOptionsItemSelectedCompat(item);
    }

    protected boolean onOptionsItemSelectedCompat(MenuItem item) {
        return false;
    }
    @Override
    public LifecycleTransformer bindLifecycle() {
        return this.bindToLifecycle();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }
    public void addContentView(int layoutResID) {
        _mContentLayout.removeAllViews();
        if (layoutResID < 1) {
            Log.e(TAG, "layout resouce id not exist");
            return;
        }
        Log.d(TAG, ">>>>>>> setContentView");
        View view = View.inflate(this, attachLayoutRes(), null);
        if (view == null) {
            setContentView(rootView);
            return;
        }
        _mContentLayout.addView(view);
        setContentView(rootView);
        initView();
    }

    @Override
    public <T extends View> T findViewById(int id) {
        return _mContentLayout.findViewById(id);
    }

    protected void showZloading(){
        if (mDialog == null) {
            mDialog = new ZLoadingDialog(this);
        }
        mDialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.RED)//颜色
                .setHintText("Loading...")
                .setHintTextSize(-1) // 设置字体大小 dp
                .setHintTextColor(Color.GRAY)  // 设置字体颜色
                .setDurationTime(0.5) // 设置动画时间百分比 - 0.5倍
                .setDialogBackgroundColor(Color.parseColor("#CC111111")) // 设置背景色，默认白色
                .show();
    }
    protected void hideZloading(){
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }
}
